﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using GenericLibrariesToolKit.BussinessException;

namespace GenericLibrariesToolKit
{
    /// <summary>
    /// 实体管理器
    /// </summary>
    public class EntityManager
    {
        //实体管理器扫描路径
        public static string scanPath;
        //存放所有实体的容器
        public Dictionary<string, Entity> entityManager;
   
        //单例模式实现代码
        #region
        /// <summary>
        /// 私有构造函数
        /// </summary>
        private EntityManager() 
        {
            //初始化实体管理器
            entityManager = new Dictionary<string, Entity>();
        }

        /// <summary>
        /// 初始化实体管理器
        /// </summary>
        /// <returns>实体管理器</returns>
        public static EntityManager CreateEntityManager()
        {
            //获取当前程序运行路径
            scanPath = AppDomain.CurrentDomain.BaseDirectory + scanPath; 
            Assembly assembly = Assembly.LoadFrom(scanPath + ".dll");
            foreach (Type type in assembly.GetTypes())
            {
                MethodInfo method = type.GetMethod("Intialization");
               
                //判断是否存在Intialization方法
                if(method != null)
                {
                    object obj = Activator.CreateInstance(type);
                    method.Invoke(obj, null);
                }
            }

            //输出实体管理器中的实体信息

            Console.WriteLine("EntityManager contains the following entitys:");
            foreach (KeyValuePair<string, Entity> pair in EntityManager.GetInsatnce().entityManager)
            {
                Console.WriteLine(pair.Key);
                
                ////
                //foreach (KeyValuePair<string, string> pair2 in pair.Value.ORMDictionary)
                //{
                //    Console.WriteLine(pair2.Key);
                //}
            }

            return EntityManagerHolder.instance;
        }

        /// <summary>
        /// 获取实体管理器的一个实例
        /// </summary>
        /// <returns>实体管理器</returns>
        public static EntityManager GetInsatnce()
        {
            return EntityManagerHolder.instance;
        }

        /// <summary>
        /// 类级内部类
        /// </summary>
        private static class EntityManagerHolder
        {
            //静态初始化器
            public static EntityManager instance = new EntityManager();
        }
        #endregion

        /// <summary>
        /// 添加一个实体
        /// </summary>
        /// <param name="className">实体的完整类名</param>
        /// <param name="entity">封装的实体对象</param>
        public void AddEntity(string className, Entity entity)
        {
            entityManager.Add(className, entity);
        }

        /// <summary>
        /// 获取一个实体
        /// </summary>
        /// <param name="className">实体的完整类名<</param>
        /// <returns>封装的实体</returns>
        public Entity GetEntity(string className)
        {
            //如果不存在className对应的实体，抛异常
            if (!entityManager.ContainsKey(className))
            {
                throw new EntityManagerException("EntityManager中不存在指定的Entity");
            }

            return entityManager[className];
        }
    }
}
